package com.xiaolin.net;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @ClassName MySocketClient
 * @Description socket 客户端
 * @Detail detail
 * @Author MyPC
 * @Date 2020/12/1
 * @Version 1.0
 */
public class MySocketClient {
    public static void main(String[] args) throws IOException {
        // 服务端套接字的监听端口
        int port=7000;
        // 服务端主机
        String host="localhost";
        // 创建一个套接字并指定其要连接的主机和端口
        Socket client=new Socket(host,port);
        DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
        DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(client.getInputStream()));
        Scanner scanner = new Scanner(System.in);
        boolean flag=false;
        while (!flag){
            double length = scanner.nextDouble();
            // 将数据传输给服务端
            dataOutputStream.writeDouble( length);
            dataOutputStream.flush();
            // 读取服务端响应结果
            double accept = dataInputStream.readDouble();
            System.out.println("计算结果：" +accept);
            while (true){
                System.out.println("想继续计算吗,Y/N");
                String an = scanner.nextLine();
                if(an.equalsIgnoreCase("y")){
                    // 向服务端写出不为0的数字，代表不让服务端关闭监听
                    dataOutputStream.writeInt(1);
                    dataOutputStream.flush();
                    break;
                }else if(an.equalsIgnoreCase("n")){
                    // 向服务端写出0的数字，代表让服务端关闭监听
                    dataOutputStream.writeInt(0);
                    dataOutputStream.flush();
                    flag=true;
                    break;
                }else {
                    System.out.println("请输入正确的回答");
                }
            }
        }
        // 关闭连接
        client.close();
    }
}
